<?php

namespace app\tool\model;

use think\facade\Db;
use app\admin\model\Common;

/**

 * 疤痕计算器模型

 * @package app\shop\model

 * @author LIU

 * @create 2021-10-01

 */

class ScarReckonerModel extends Common
{
    // use ActionRecordTrait;

    /**

     * 表名

     * @var string

     */

    protected $name = 'scar_reckoner';

    protected $defaultSoftDelete = 0;

    /**

     * 获取计算器基础信息

     * @param  array $request 请求参数

     * @return array

     * @author LIU

     * @create 2021-10-26

     */

    public function getContent()
    {
        $where = [];

        array_push($where,['type','=','1']);

        $first = Db::name('scar_reckoner')->where($where)->column('id,title');

        // echo "<pre>";
        // print_r($first);
        // echo "</pre>";
        // exit;

        // ///////////////////////////////////////////////////////////////////////////////////////////////////
        

        // $list = [];

        // foreach ($first as $key => $value) {

        //     $one = [];

        //     $one['firstTitle'] = $value['title'];

        //     $where = [];

        //     array_push($where,['type','=','2']);

        //     array_push($where,['pid','=',$value['id']]);

        //     $second = Db::name('scar_reckoner')->where($where)->select()->toArray();

        //     foreach ($second as $k => $v) {
                
        //         $two = [];

        //         $two['id'] = $v['id'];

        //         $two['title'] = $v['title'];

                
        //     }

            

        // }

        // echo "<pre>";
        // print_r($second);
        // echo "</pre>";
        // exit;

        return [

            'data' => $first,

        ];
    }

    public function getSecond($request)
    {

        $firstId = $request['firstId'];

        $where = [];

        array_push($where,['pid','=',$firstId]);

        $second = Db::name('scar_reckoner')->where($where)->select()->toArray();

        return [

            'data' => $second,

        ];
    }

    public function scarReckonerSubmit($request)
    {
        $type3 = $request['type3'];

        if($request['checked'] == 'true'){

            $discount = '0.85';

        }else{

            $discount = '1';

        }

        $cureNum = $request['cureNum'] ?? '1';

        if($request['length']>0){

            $num = $request['length'];

        }else{

            $num = $request['area'];

        }

        $where = [];

        array_push($where,['pid','=',$type3]);

        $rule = Db::name('scar_reckoner_rule')->where($where)->order('phase')->select()->toArray();

        // echo "<pre>";
        // print_r($rule);
        // echo "</pre>";
        // exit;

        $finalprice = '0.00';

        $lastMark = '';

        foreach ($rule as $key => $value) {
            // echo $value['mark']."<br/>";
            
            if($value['mark']>0){

                if($num>$value['mark']){

                    if($value['price_type'] == '1'){

                        $finalprice = bcadd($finalprice,$value['price'],2);

                    }else{

                        $sub = bcsub($value['mark'],$lastMark,2);

                        $mul = ceil(bcdiv($sub,$value['increment'],2));

                        $addPrice = bcmul($mul,$value['price'],2);

                        $finalprice = bcadd($finalprice,$addPrice,2);

                        // $num = bcsub($num,$value['mark'],2);

                    }

                    $lastMark = $value['mark'];

                    

                }else{

                    $sub = bcsub($num,$lastMark,2);

                    if($sub > '0.00'){

                        if($value['price_type'] == '1'){

                            $finalprice = bcadd($finalprice,$value['price'],2);
    
                        }else{
    
                            
    
                            $mul = ceil(bcdiv($sub,$value['increment'],2));
    
                            $addPrice = bcmul($mul,$value['price'],2);
    
                            $finalprice = bcadd($finalprice,$addPrice,2);
    
                        }

                        $lastMark = $value['mark'];

                    }

                }

            }else{

                // $sub = bcsub($num,$lastMark,2);

                // if($sub > '0.00'){

                    if($value['price_type'] == '1'){

                        $finalprice = bcadd($finalprice,$value['price'],2);
    
                    }

                // }

                

            }

            // var_dump($finalprice);
            // echo "<br/>";

        }

        // exit;
        // $cureNum
        $finalprice = bcmul($finalprice,$discount,2);

        $finalprice = bcmul($finalprice,$cureNum,2);

        return $finalprice;
        
    }
}

?>